﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Business {
    public class Resume {
        public string PhoneNumber { get; set; }

        public string MessageId { get; set; }

        public string Name { get; set; }

        public string Position { get; set; }

        public string Gender { get; set; }

        public int? BirthYear { get; set; }

        public string Marriage { get; set; }

        public int? YearsOfWorking { get; set; }

        public string HuKou { get; set; }

        /// <summary>
        /// 学历
        /// </summary>
        public string Education { get; set; }

        /// <summary>
        /// 专业
        /// </summary>
        //public string Major { get; set; }

        /// <summary>
        /// 期望月薪
        /// </summary>
        public int? Salary { get; set; }

        public string HtmlBody { get; set; }

        public string TextBody { get; set; }

        public IList<DateTime> WorkFrom { get; set; }

        public IList<DateTime> WorkTo { get; set; }

        public IList<string> WorkCompanies { get; set; }

        public IList<string> WorkPositions { get; set; }



        public Resume() {
            WorkFrom = new List<DateTime>();
            WorkTo = new List<DateTime>();
            WorkCompanies = new List<string>();
            WorkPositions = new List<string>();
        }

        public void AddWork(DateTime? from, DateTime? to, string company, string position) {
            WorkFrom.Add(from ?? default(DateTime));
            WorkTo.Add(to ?? default(DateTime));
            WorkCompanies.Add(company);
            WorkPositions.Add(position);
        }

        public override string ToString() {
            return string.Join(Environment.NewLine, Inspect());
        }

        public IEnumerable<string> Inspect() {
            Type t = this.GetType();
            foreach (var pi in t.GetProperties()) {
                var value = pi.GetValue(this, null);
                if (value is string) {
                    var str = (string)value;
                    if (str.Length > 60)
                        str = str.Substring(0, 60) + "...[" + str.Length + "]";
                    yield return string.Format("{0}={1}", pi.Name, str);
                }
                else
                    yield return string.Format("{0}={1}", pi.Name, value);
            }
        }

        public void Trim() {
            Type t = this.GetType();
            foreach (var pi in t.GetProperties()) {
                var value = pi.GetValue(this, null);
                if (value is string) {
                    var str = (string)value;
                    if (str != null && pi.CanWrite)
                        pi.SetValue(this, str.Trim());
                }
            }
        }

        public string GetShortSalary() {
            if (Salary.HasValue)
                return string.Format("{0:0}k", Salary / 1000);
            return "面议";
        }

        public string GetRecentWorkDuration() {
            if (WorkFrom.Count > 0 && WorkTo.Count > 0) {
                var recentFrom = WorkFrom[0];
                var recentTo = WorkTo[0];
                var to = recentTo != default(DateTime) ? recentTo : DateTime.Today;
                var duration = to - recentFrom;
                return duration.Friendly();
            }
            return null;
        }

        public string GetRecentWorkCompanyShortName() {
            var name = WorkCompanies.FirstOrDefault();
            if (name != null)
                return Regex.Replace(name, @"(股份)?(科技)?(发展)?有限公司", string.Empty);
            return name;
        }

        public string GetRecentWorkPosition() {
            return WorkPositions.FirstOrDefault();
        }

        public static IEnumerable<string> GetAllProperties() {
            Type t = typeof(Resume);
            foreach (var pi in t.GetProperties()) {
                yield return pi.Name;
            }
        }

        public static IEnumerable<string> GetAllPropertiesWithoutBody() {
            return GetAllProperties().Except(new[] { "HtmlBody", "TextBody" });
        }
    }
}
